from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# 打开浏览器
driver = webdriver.Chrome()
# 最大化
driver.maximize_window()
# 请求163网址
driver.get('https://mail.163.com')
# 准备数据
code = [
    {
        'name': 'NTES_SESS',
        'value': '_f1_gW2m1j1cUaJSSDDIYtXYXUfJoDREVzqSL9SsYwwHYtclYUOmhkQcHqpVwBRgLgNpsmmtPseyzkt.tgn1FS93sVWM6CSd.odRCBARARmGMuiHu4X.kBbMu7s10DmDA0nugVqB72TmCbqVT8fQNe7fdussNju.gmPBD5U_5uivGoGLekE4hpDpuyOao6IxJ_8WlZnMzgRoaDQp1wSTUOG7E'
    },
    {
        'name': 'Coremail',
        'value': 'b4a7732c5545b%cAOpAQeSqxSYQJnSYbSSqIKpaMFSlyGi%g3a26.mail.163.com'
    },
    {
        'name': 'MAIL_ENTRY_INFO',
        'value': '1|28|mail163|mail163_letter|117.155.142.223||'
    },
    {
        'name': 'MAIL_ENTRY_CS',
        'value': '1487ab7bc3ac2733528cea3ae720c01f'
    },
    {
        'name': 'cm_last_info',
        'value': 'dT1saGwxMzA5NzM1NzE1JTQwMTYzLmNvbSZkPWh0dHBzJTNBJTJGJTJGbWFpbC4xNjMuY29tJTJGanM2JTJGbWFpbi5qc3AlM0ZzaWQlM0RjQU9wQVFlU3F4U1lRSm5TWWJTU3FJS3BhTUZTbHlHaSZzPWNBT3BBUWVTcXhTWVFKblNZYlNTcUlLcGFNRlNseUdpJmg9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20lMkZqczYlMkZtYWluLmpzcCUzRnNpZCUzRGNBT3BBUWVTcXhTWVFKblNZYlNTcUlLcGFNRlNseUdpJnc9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20mbD0tMSZ0PS0xJmFzPXRydWU='
    },
]
# 依次添加cookie
driver.add_cookie(code[0])
driver.add_cookie(code[1])
driver.add_cookie(code[2])
driver.add_cookie(code[3])
driver.add_cookie(code[4])
# 刷新
driver.refresh()
# 点击写信按钮
time.sleep(2)
driver.find_element(By.ID, '_mail_component_149_149').click()
# 找到收件人框
time.sleep(2)
driver.find_element(By.CLASS_NAME, 'nui-editableAddr-ipt').send_keys('lhl1309735715@163.com')
# 找到主题框
time.sleep(2)
ipts = driver.find_elements(By.XPATH, '//input[@class="nui-ipt-input"]')[2]
ipts.send_keys('111')
# 切换frame
time.sleep(2)
frame = driver.find_element(By.CLASS_NAME, 'APP-editor-iframe')
driver.switch_to.frame(frame)
# 定位输入文本框,输入内容
time.sleep(2)
driver.find_element(By.CLASS_NAME, 'nui-scroll').send_keys('1111')
# 切换到主页面
time.sleep(2)
driver.switch_to.default_content()
# 定位发送标签点击
time.sleep(2)
btns = driver.find_elements(By.CLASS_NAME, 'nui-btn-text')[-2]
btns.click()
# 关闭浏览器
time.sleep(2)
driver.quit()